﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DSheet.Core.Data
{
    /// <summary>
    /// 文档定义
    /// </summary>
    public class DocDef
    {
        public Guid Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }

        public ComboDim YDims { get; set; }
        public ComboDim XDims { get; set; }

        public DocDef()
        {
            YDims = new ComboDim();
            XDims = new ComboDim();
        }

        public void RefreshDimIndex()
        {
            for (int index = 0; index < YDims.Count; index++)
            {
                DocDim dim = YDims[index];
                dim.Index = index;
                dim.DirIndex = index;
            }
            for (int index = 0; index < XDims.Count; index++)
            {
                DocDim dim = XDims[index];
                dim.Index = index;
                dim.DirIndex = YDims.Count + index;
            }
        }

        /// <summary>
        /// 设置为页面维度
        /// </summary>
        /// <param name="dimId"></param>
        public void SetPageDim(Guid dimId)
        {
            for (int index = YDims.Count - 1; index >= 0; index--)
            {
                var dim = YDims[index];
                if (dim.Id == dimId)
                {
                    dim.IsPageDim = true;
                    return;
                }
            }

            for (int index = XDims.Count - 1; index >= 0; index--)
            {
                var dim = XDims[index];
                if (dim.Id == dimId)
                {
                    dim.IsPageDim = true;
                    return;
                }
            }
        }

        public void ClearPageDim(Guid dimId)
        {
            for (int index = YDims.Count - 1; index >= 0; index--)
            {
                var dim = YDims[index];
                if (dim.Id == dimId)
                {
                    dim.IsPageDim = false;
                    return;
                }
            }

            for (int index = XDims.Count - 1; index >= 0; index--)
            {
                var dim = XDims[index];
                if (dim.Id == dimId)
                {
                    dim.IsPageDim = false;
                    return;
                }
            }
        }

        public void SetXDirection(Guid dimId)
        {
            for (int index = YDims.Count - 1; index >= 0; index--)
            {
                var dim = YDims[index];
                if (dim.Id == dimId)
                {
                    YDims.RemoveAt(index);
                    dim.Direction = DimDirection.X;
                    XDims.Add(dim);
                    break;
                }
            }

            RefreshDimIndex();
        }

        public void SetYDirection(Guid dimId)
        {
            for (int index = 0; index < XDims.Count; index++)
            {
                var dim = XDims[index];
                if (dim.Id == dimId)
                {
                    XDims.RemoveAt(index);
                    dim.Direction = DimDirection.Y;
                    YDims.Add(dim);
                    break;
                }
            }

            RefreshDimIndex();
        }

        public void Up(Guid dimId)
        {
            DocDim hitDim = null;
            for (int index = 0; index < YDims.Count; index++)
            {
                if (YDims[index].Id == dimId)
                {
                    hitDim = YDims[index];
                    YDims.RemoveAt(index);
                    int insertIndex = index + 1;
                    if (insertIndex > YDims.Count - 1)
                    {
                        insertIndex = YDims.Count - 1;
                    }
                    YDims.Insert(insertIndex, hitDim);
                    RefreshDimIndex();
                    return;
                }
            }

            for (int index = 0; index < XDims.Count; index++)
            {
                if (XDims[index].Id == dimId)
                {
                    hitDim = XDims[index];
                    XDims.RemoveAt(index);
                    int insertIndex = index + 1;
                    if (insertIndex > XDims.Count - 1)
                    {
                        insertIndex = XDims.Count - 1;
                    }
                    XDims.Insert(insertIndex, hitDim);
                    RefreshDimIndex();
                    return;
                }
            }
        }

        public void Down(Guid dimId)
        {
            DocDim hitDim = null;
            for (int index = 0; index < YDims.Count; index++)
            {
                if (YDims[index].Id == dimId)
                {
                    hitDim = YDims[index];
                    YDims.RemoveAt(index);
                    int insertIndex = index - 1;
                    if (insertIndex < 0)
                    {
                        insertIndex = 0;
                    }
                    YDims.Insert(insertIndex, hitDim);
                    RefreshDimIndex();
                    return;
                }
            }

            for (int index = 0; index < XDims.Count; index++)
            {
                if (XDims[index].Id == dimId)
                {
                    hitDim = XDims[index];
                    XDims.RemoveAt(index);
                    int insertIndex = index - 1;
                    if (insertIndex < 0)
                    {
                        insertIndex = 0;
                    }
                    XDims.Insert(insertIndex, hitDim);
                    RefreshDimIndex();
                    return;
                }
            }
        }

        public List<DocDim> GetPageDims()
        {
            List<DocDim> pageDims = new List<DocDim>();
            foreach (var item in YDims)
            {
                if (item.IsPageDim)
                {
                    pageDims.Add(item);
                }
            }
            foreach (var item in XDims)
            {
                if (item.IsPageDim)
                {
                    pageDims.Add(item);
                }
            }
            return pageDims;
        }
    }

    /// <summary>
    /// 模板上使用的维度信息
    /// </summary>
    public class DocDim
    {
        /// <summary>
        /// 是否为页面维
        /// </summary>
        public bool IsPageDim { get; set; }

        public Guid Id { get; set; }
        public Guid DimId { get; set; }
        public string DimCode { get; set; }
        public string DimName { get; set; }

        /// <summary>
        /// 维度索引
        /// </summary>
        public int Index { get; set; }

        public DimDirection Direction { get; set; }

        public int DirIndex { get; set; }

        ///// <summary>
        ///// 计算维度树
        ///// </summary>
        ///// <returns></returns>
        //public DTree CalcDTree(DContext context)
        //{
        //    DimData data = new DimDataService().GetDimData(context.DBContext, DimId);
        //    DTree tree = data.ConvertToTree();
        //    //tree.Table = this;

        //    Stack<DTreeNode> stack = new Stack<DTreeNode>();
        //    foreach (var node in tree.Nodes)
        //    {
        //        stack.Push(node);
        //    }

        //    if (NextDim != null)
        //    {
        //        while (stack.Count > 0)
        //        {
        //            DTreeNode node = stack.Pop();
        //            node.NextTree = NextDim.GetRelateTree(context, node);

        //            foreach (var subNode in node.Nodes)
        //            {
        //                stack.Push(subNode);
        //            }
        //        }
        //    }
        //    else
        //    {
        //        while (stack.Count > 0)
        //        {
        //            DTreeNode node = stack.Pop();
        //            foreach (var subNode in node.Nodes)
        //            {
        //                stack.Push(subNode);
        //            }
        //        }
        //    }

        //    return tree;
        //}

        //public DTree GetRelateTree(DContext context, DTreeNode node)
        //{
        //    DTree tree = OnGetTree(context, node);
        //    tree.Level = node.Tree.Level + 1;

        //    if (NextRelation != null)
        //    {
        //        Stack<DTreeNode> nodes = new Stack<DTreeNode>();

        //        for (int index = 0; index < tree.Nodes.Count; index++)
        //        {
        //            nodes.Push(tree.Nodes[index]);
        //        }

        //        while (nodes.Count > 0)
        //        {
        //            DTreeNode treeNode = nodes.Pop();
        //            treeNode.NextTree = NextRelation.GetTree(context, treeNode);

        //            for (int index = 0; index < treeNode.Nodes.Count; index++)
        //            {
        //                nodes.Push(treeNode.Nodes[index]);
        //            }
        //        }
        //    }
        //    return null;
        //}

        /// <summary>
        /// 获取维度数据
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public DimData GetData(DContext context)
        {
            DimData data = new DimDataService().GetDimData(context.DBContext, DimId);
            return data;
        }
    }
}
